package com.weiyuwqj.admin.controller.admin.invite;

import com.weiyuwqj.api.service.BusInvestService;
import com.weiyuwqj.api.service.BasUserInviteIncomeService;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Message;

import com.weiyuwqj.api.model.convert.ModelConvert;
import com.weiyuwqj.api.model.vo.BasUserInviteIncomeVo;
import com.weiyuwqj.api.model.vo.BusItemInvestVo;
import com.weiyuwqj.api.util.DateUtils;
import com.weiyuwqj.api.util.ExcelUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by Yao xiuwei on 2015/12/2.
 */
@Controller
@RequestMapping(value="/admin/inviteIncome")
public class BasUserInviteIncomeController {
    @Autowired
    BasUserInviteIncomeService inviteIncomeService;

    @Autowired
    BusInvestService busInvestService;
    //显示&查询
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object list(Paginator paginator,HttpServletRequest request){
        String username = request.getParameter("username");
        String realname = request.getParameter("realname");
        String mobile = request.getParameter("mobile");
        String beginDateTime = request.getParameter("beginDateTime");
        String endDateTime = request.getParameter("endDateTime");
        Map<String, Object> map = new HashMap<String, Object>();
        if(StringUtils.isNotEmpty(username)){
            map.put("username", username);
        }
        if(StringUtils.isNotEmpty(realname)){
            map.put("realname", realname);
        }
        if(StringUtils.isNotEmpty(mobile)){
            map.put("mobile", mobile);
        }
        if(StringUtils.isNotEmpty(beginDateTime)){
            map.put("beginDateTime", beginDateTime);
        }
        if(StringUtils.isNotEmpty(endDateTime)){
            map.put("endDateTime", endDateTime);
        }
        return inviteIncomeService.getList(paginator, map);
    }

    //导出投资信息
    @ResponseBody
    @RequestMapping("/excel")
    public Object excelInvest(HttpServletRequest request,
                              String username,
                              String realname,
                              String mobile,
                              String beginDateTime,
                              String endDateTime) {
        String[] cArray = {"用户名", "手机号", "真实姓名", "收益(元)", "投资人用户名", "投资人手机号","投资人姓名","投资项目", "投资金额","投资期限","添加时间"};
        String[] eArray = {"Username", "Mobile", "Realname", "Income", "InviteUsername","InviteMobile","InviteRealname", "ItemName", "InvestDealAmount","ItemCycle","Addtime"};
        ModelConvert[] converts = {null, null, null, null, null, null, null ,null,null, null,null};

        List<String> cList = Arrays.asList(cArray);
        List<String> eList = Arrays.asList(eArray);
        List<ModelConvert> convertList = Arrays.asList(converts);
        Map map = new HashMap();
        if(StringUtils.isNotEmpty(username)){
            map.put("username", username);
        }
        if(StringUtils.isNotEmpty(realname)){
            map.put("realname", realname);
        }
        if(StringUtils.isNotEmpty(mobile)){
            map.put("mobile", mobile);
        }
        if(StringUtils.isNotEmpty(beginDateTime)){
            map.put("beginDateTime", beginDateTime);
        }
        if(StringUtils.isNotEmpty(endDateTime)){
            map.put("endDateTime", endDateTime);
        }

        List<BasUserInviteIncomeVo> contentList =  inviteIncomeService.getList(map);

        String serverDir = request.getServletContext().getRealPath("/") + "admin/export/";
        //String excelFilePath=ExcelUtils.export(allList,serverDir);
        String excelFilePath = ExcelUtils.export("用户邀请收益列表", cList, eList, convertList, contentList, serverDir);

        HashMap ret = Message.OK;
        ret.put("file", "export/" + excelFilePath);
        return ret;
    }

    //获取编辑信息
    @ResponseBody
    @RequestMapping(value = "/getSingle",method = RequestMethod.POST)
    public Object getSingle(Integer id) {
        return inviteIncomeService.getSingle(id);
    }

}
